package com.ruoyi.consumer.aop.convert;

import com.ruoyi.consumer.domain.*;
import com.ruoyi.consumer.service.impl.*;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class WxOrderOperateStrategy implements ConvertStrategy {

    @Autowired
    private CCarServiceImpl cCarService;

    @Autowired
    private COrderServiceImpl cOrderService;

    @Autowired
    private CUserServiceImpl cUserService;

    @Autowired
    private CDeviceOrdersServiceImpl cDeviceOrdersService;

    @Autowired
    private CFillingFacilitServiceImpl cFillingFacilitService;

    @Autowired
    private CStatisticTableServiceImpl cStatisticTableService;

    @Override
    public Object getWxOrderOperate(String weixinOrderId) {
        System.out.println("微信订单切面" + weixinOrderId);
        CDeviceOrders cDeviceOrders = new CDeviceOrders();
        cDeviceOrders.setWeixinOrderId(weixinOrderId);
        List<CDeviceOrders> realOrders = cDeviceOrdersService.selectCDeviceOrdersList(cDeviceOrders);
        if (realOrders.size() == 0) {
            System.out.println("实际订单orderId不存在");
            return getWxOrderOperate(weixinOrderId);
        }
        CDeviceOrders realOrder = realOrders.get(0);
        COrder cOrder = new COrder();
        cOrder.setOrderNum(weixinOrderId);
        List<COrder> cOrders = cOrderService.selectCOrderList(cOrder);
        if (cOrders.size() == 0) {
            System.out.println("微信订单不存在");
            return getWxOrderOperate(weixinOrderId);
        }
        //获取加注机信息
        CFillingFacilit cFillingFacilit = new CFillingFacilit();
        cFillingFacilit.setFacilitSn(realOrder.getSn());
        List<CFillingFacilit> cFillingFacilits = cFillingFacilitService.selectCFillingFacilitList(cFillingFacilit);
        CFillingFacilit cFacilit = cFillingFacilits.get(0);
        COrder wxOrder = cOrders.get(0);
        CUser cUser = cUserService.selectCUserByUserId(wxOrder.getTargetUserId());
        //获取车辆id
        CCar cCar = cCarService.selectCCarByCarId(cUser.getCarId());
        String plateNum = null;
        Integer carId = null;
        if (cCar != null) {
            plateNum = cCar.getPlateNum();
            carId = cCar.getCarId();
        }
        //获取用户姓名
        String userName = null;
        if (cUser.getUserName() != null && !cUser.getUserName().equals("")) {
            userName = cUser.getUserName();
        } else {
            userName = cUser.getUserPhone();
        }

        //赋值给统计表
        CStatisticTable cStatisticTable = new CStatisticTable();
        cStatisticTable.setSn(realOrder.getSn());
        cStatisticTable.setFacilitId(cFacilit.getFacilitId());
        cStatisticTable.setUserId(cUser.getUserId());
        cStatisticTable.setUserName(userName);
        cStatisticTable.setFillerId(realOrder.getFillerId());
        cStatisticTable.setOrderPrice(realOrder.getOrderPrice());
        cStatisticTable.setCarId(carId);
        cStatisticTable.setPlateNum(plateNum);
        cStatisticTable.setStatisticType(3);
        cStatisticTable.setWeixinOrderId(weixinOrderId);
        cStatisticTable.setFacilitProvince(cFillingFacilit.getFacilitProvince());
        cStatisticTable.setFacilitCity(cFillingFacilit.getFacilitCity());
        cStatisticTable.setFacilitArea(cFillingFacilit.getFacilitArea());
        cStatisticTable.setFacilitLat(cFillingFacilit.getFacilitLat());
        cStatisticTable.setFacilitLng(cFillingFacilit.getFacilitLng());
        cStatisticTable.setOrderFilled(0L);
        cStatisticTable.setOrderMoney(0);
        cStatisticTable.setOrderPrice(0);

        int i = cStatisticTableService.insertCStatisticTable(cStatisticTable);
        if (i > 0) {
            System.out.println("微信下单统计成功" + weixinOrderId);
            return weixinOrderId;
        }
        return weixinOrderId;
    }

    @Override
    public Object getWxMinOrderOperate(Long orderId, String out_trade_no) {
        System.out.println("微信小订单切面" + orderId);
        CStatisticTable cStatisticTable = new CStatisticTable();
        cStatisticTable.setFillerId(orderId.toString());
        List<CStatisticTable> cStatisticTables = cStatisticTableService.selectCStatisticTableList(cStatisticTable);
        CDeviceOrders cDeviceOrders = new CDeviceOrders();
        cDeviceOrders.setFillerId(orderId.toString());
        List<CDeviceOrders> realOrders = cDeviceOrdersService.selectCDeviceOrdersList(cDeviceOrders);
        COrder cOrder = new COrder();
        cOrder.setOrderNum(out_trade_no);
        List<COrder> cOrders = cOrderService.selectCOrderList(cOrder);
        if (cOrders.size() == 0) {
            System.out.println("微信订单orderId不存在");
            return getWxMinOrderOperate(orderId, out_trade_no);
        }
        if (realOrders.isEmpty()) {
            Assert.notNull(realOrders, "实际订单不存在");
        }
        if (cStatisticTables.isEmpty()) {
            Assert.notNull(cStatisticTables, "统计表不存在");
        }
        COrder wxOrder = cOrders.get(0);
        CStatisticTable statisticTable = cStatisticTables.get(0);
        statisticTable.setOrderFilled(wxOrder.getOrderAmount());
        statisticTable.setOrderMoney(wxOrder.getOrderPay());
        statisticTable.setOrderPrice(realOrders.get(0).getOrderPrice());
        int i = cStatisticTableService.updateCStatisticTable(statisticTable);
        if (i > 0) {
            System.out.println("微信小订单统计成功");
            return null;
        }

        return orderId;
    }


    @Override
    public Object takeOrderOperate(Integer facilitId, Integer userId, Long amount, String snNumber, Long orderId) {
        return null;
    }

    @Override
    public Object getMinOrderOperate(Long orderId) {
        return null;
    }
}
